﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace TowerDef_s_enseLibrary.tower.slowtower
{
    class SlowTower : Tower
    {
        #region variables

        // Defines how fast an enemy will move when hit.
        private float _speedModifier;
        // Defines how long this effect will last.
        private float _modifierDuration;

        #endregion variables

        #region functions

        /// <summary>
        /// Create a slow tower
        /// </summary>
        /// <param name="texture">The slow tower Texture</param>
        /// <param name="bulletTexture">The bullet Texture</param>
        /// <param name="position">The tower position</param>
        public SlowTower(Texture2D texture, Texture2D bulletTexture, Vector2 position) : base(texture, bulletTexture, position)
        {
            this._speedModifier = 0.6f;
            this._modifierDuration = 2.0f;
            this.damage = 15;
            this.cost = 15;
            this.radius = 80;
        }

        /// <summary>
        /// Updates the slow tower in matters of the game time
        /// </summary>
        /// <param name="gameTime">The current game time</param>
        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
            if(bulletTimer >= 0.75f && target != null)
            {
                var bullet = new Bullet(bulletTexture, Vector2.Subtract(center, new Vector2(bulletTexture.Width / 2)), rotation, 6, damage);

                bulletList.Add(bullet);
                bulletTimer = 0;
            }

            for(int i = 0; i < bulletList.Count; i++)
            {
                Bullet bullet = bulletList[i];
                bullet.SetRotation(rotation);
                bullet.Update(gameTime);

                if(!IsInRange(bullet.Center))
                    bullet.Kill();

                if(target != null && Vector2.Distance(bullet.Center, target.Center) < 12)
                {
                    target.CurrentHealth -= bullet.Damage;
                    bullet.Kill();
                    // Apply our speed modifier if it is better than
                    // the one currently affecting the target :
                    if(target.SpeedModifier <= _speedModifier)
                    {
                        target.SpeedModifier = _speedModifier;
                        target.ModifierDuration = _modifierDuration;
                    }
                }

                if(bullet.IsDead())
                {
                    bulletList.Remove(bullet);
                    i--;
                }
            }
        }

        #endregion functions

        #region events



        #endregion events
    }
}
